package com.jstyles.jcring.healthlib.host;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.EventChannel;


public class MainActivity extends FlutterActivity {

    private static final String NativeCallCHANNEL = "jcring_call";
    private static final String NativeResultCHANNEL = "jcring_result";
    private static final String TAG = MainActivity.class.getSimpleName();
    public static final String spName = "jstyle_jcring_newapp_18";

    private SharedPreferences sharedPreferences;
    public boolean isShowHealthMain = false;
    public boolean isGoTargetSetting = false;
    MethodChannel resultMethodChannel;

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        resultMethodChannel = new MethodChannel(Objects.requireNonNull(getFlutterEngine()).getDartExecutor().getBinaryMessenger(), NativeResultCHANNEL);
        sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE);
        isShowHealthMain = getSpBoolean("health_data");

        if(isGoTargetSetting){
            if (resultMethodChannel != null) {
                resultMethodChannel.invokeMethod("set_women_conception_data", null);
            }
        }else{
            if (isShowHealthMain) {
                initHealthData();
            } else {
                initShowFirstHealth();
            }
        }
        new MethodChannel(Objects.requireNonNull(getFlutterEngine()).getDartExecutor().getBinaryMessenger(), NativeCallCHANNEL).setMethodCallHandler(
                new MethodChannel.MethodCallHandler() {
                    @Override
                    public void onMethodCall(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
                        switch (methodCall.method) { //关闭当前界面
                            case "back_pressed":
                                Log.i(TAG, "FlutterHealthSettingActivity 收到的Flutter的退出信息....");
                                onBackPressed();
                                break;

                            case "read_user_id":
                                Log.i(TAG, "FlutterHealthSettingActivity 收到的Flutter的获取用户ID....");
                                HashMap<String, Object> dataMap = new HashMap<>();
                                dataMap.put("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiIwNTNhNTlkNGJjZWQyMjE1OWFmNmY0ODJjOWRlNjI1OWRlMDFlZGU1MzVmYTI0OTdhZmM4MTciLCJleHAiOjE3NjA4NDI4MTl9.9ko1rTIi5K1SZyVrg4Vz-jr0mCj6zAL2Dqws6eY9XlQ");
                                if (resultMethodChannel != null) {
                                    resultMethodChannel.invokeMethod("read_user_id", dataMap);
                                }
                                break;

                            case "init_women_health_data":
                                Log.i(TAG, "FlutterHealthSettingActivity 收到的Flutter的初始化女性健康信息数据...");
                                if(isGoTargetSetting){
                                    if (resultMethodChannel != null) {
                                        resultMethodChannel.invokeMethod("set_women_conception_data", null);
                                    }
                                }else{
                                    if (isShowHealthMain) {
                                        initHealthData();
                                    } else {
                                        initShowFirstHealth();
                                    }
                                }
                                break;

                            case "set_women_health_data":
                                isShowHealthMain = true;
                                setSpBoolean("health_data", true);
                                Map<String, Object> healthMap = methodCall.arguments();
                                Log.i(TAG, "FlutterHealthSettingActivity 收到的Flutter的已经设置女性健康信息healthMap：" + healthMap);
                                setSpString("menstrual_time", (String) healthMap.get("health_menstrual_time"));
                                setSpInteger("cycle_value", (int) healthMap.get("health_cycle_value"));
                                setSpInteger("period_length", (int) healthMap.get("health_period_length"));
                                setSpInteger("periodic_law", (int) healthMap.get("health_regularly"));
                                setSpInteger("target", (int) healthMap.get("health_tracking"));
                                break;

                            case "write_health_history":
                                String historyList = methodCall.arguments();
                                setSpString("health_history", historyList);
                                Log.i(TAG, "FlutterHealthSettingActivity write key:health_history value:" + historyList);
                                break;

                            case "read_health_history":
                                if (resultMethodChannel != null) {
                                    resultMethodChannel.invokeMethod("read_health_history", getSpString("health_history"));
                                }
                                Log.i(TAG, "FlutterHealthSettingActivity read key:health_history value:" + getSpString("health_history"));
                                break;


                            case "set_women_conception_data":
                                Log.i(TAG, "FlutterHealthSettingActivity 收到的Flutter跳转到孕期原生UI" );
                                break;

                            case "reset_women_conception_data":
                                Log.i(TAG, "FlutterHealthSettingActivity 收到的Flutter跳转设置孕期数据界面" );
                                break;

                            case "set_women_prediction_data":
                                Log.i(TAG, "FlutterHealthSettingActivity 收到的Flutter跳转到月经周期FlutterUI" );
                                isGoTargetSetting = false;
                                setSpInteger("target", 1);
                                initHealthData();
                                break;

                        }
                    }
                }
        );
    }

    public void initHealthData() {

        Map<String, Object> temperatureMap = new HashMap<>();
        temperatureMap.put("data", new double[]{0.01, 0.01, 0.0, 0.0, 0.0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,-0.01, -0.02, 0.01, 0.01});
        temperatureMap.put("temp_type", 1);
        Log.i(TAG, "FlutterHealthSettingActivity 收到的Flutter的已经设置女性健康温度temperatureMap：" + temperatureMap);
        if (resultMethodChannel != null) {
            resultMethodChannel.invokeMethod("read_temperature_data", temperatureMap);
        }

        String menstrualTime = getSpString("menstrual_time");
        int cycleValue = getSpInteger("cycle_value");
        int periodLength = getSpInteger("period_length");
        int periodicLaw = getSpInteger("periodic_law");
        int target = getSpInteger("target");

        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("health_menstrual_time", menstrualTime);
        dataMap.put("health_cycle_value", cycleValue);
        dataMap.put("health_period_length", periodLength);
        dataMap.put("health_regularly", periodicLaw);
        dataMap.put("health_tracking", target);

        if (resultMethodChannel != null) {
            resultMethodChannel.invokeMethod("init_women_health_data", dataMap);
        }

        HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiIwNTNhNTlkNGJjZWQyMjE1OWFmNmY0ODJjOWRlNjI1OWRlMDFlZGU1MzVmYTI0OTdhZmM4MTciLCJleHAiOjE3NjA4NDI4MTl9.9ko1rTIi5K1SZyVrg4Vz-jr0mCj6zAL2Dqws6eY9XlQ");
        if (resultMethodChannel != null) {
            resultMethodChannel.invokeMethod("read_user_id", tokenMap);
        }

        if (resultMethodChannel != null) {
            resultMethodChannel.invokeMethod("read_health_history", getSpString("health_history"));
        }

        Map<String, Object> hrvMap = new HashMap<>();
        hrvMap.put("data", new double[]{180, 120, 100, 80, 280, 40, 20, 0});
        Log.i(TAG, "FlutterHealthSettingActivity 收到的Flutter的已经设置女性健康心率变异性hrvMap：" + hrvMap);
        if (resultMethodChannel != null) {
            resultMethodChannel.invokeMethod("read_hrv_data", hrvMap);
        }

        Map<String, Object> hrMap = new HashMap<>();
        hrMap.put("data", new double[]{140, 120, 100, 80, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140, 60, 40, 20, 0, 140});
        Log.i(TAG, "FlutterHealthSettingActivity 收到的Flutter的已经设置女性健康心率hrMap：" + hrMap);
        if (resultMethodChannel != null) {
            resultMethodChannel.invokeMethod("read_hr_data", hrMap);
        }
    }

    public void initShowFirstHealth() {
        if (resultMethodChannel != null) {
            resultMethodChannel.invokeMethod("init_women_health_data", null);
        }
    }

    public void setSpBoolean(String name, boolean value) {
        if (sharedPreferences != null) {
            sharedPreferences.edit().putBoolean(name, value).apply();
            Log.i(TAG, "FlutterHealthSettingActivity setSpBoolean name: " + name + " value: " + value);
        }
    }

    public boolean getSpBoolean(String name) {
        return sharedPreferences.getBoolean(name, false);
    }

    public void setSpString(String name, String value) {
        if (sharedPreferences != null) {
            sharedPreferences.edit().putString(name, value).apply();
            Log.i(TAG, "FlutterHealthSettingActivity setSpString name: " + name + " value: " + value);
        }
    }

    public String getSpString(String name) {
        return sharedPreferences.getString(name, "");
    }

    public void setSpInteger(String name, int value) {
        if (sharedPreferences != null) {
            sharedPreferences.edit().putInt(name, value).apply();
            Log.i(TAG, "FlutterHealthSettingActivity setSpInteger name: " + name + " value: " + value);
        }
    }

    public int getSpInteger(String name) {
        return sharedPreferences.getInt(name, 0);
    }

    public void setSpLong(String name, long value) {
        if (sharedPreferences != null) {
            sharedPreferences.edit().putLong(name, value).apply();
            Log.i(TAG, "FlutterHealthSettingActivity setSpLong name: " + name + " value: " + value);
        }
    }

    public long getSpLong(String name) {
        return sharedPreferences.getLong(name, 0L);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
